<?php
namespace app\home\controller;

//引入框架的控制器
use think\Controller;

//引入Cookie
use think\Cookie;

class Index extends Controller
{
    //构造函数
    public function __construct(){
        //手动把父类的方法获取过来
        parent::__construct();

        //在控制器的全局中保留一个全局变量
        //app/common/Model/User/User.php
        $this->BusinessModel = model('Business.Business');
        $this->SubjectModel = model('Subject.Subject');
        $this->CommentModel = model('Subject.Comment');
    }

    public function index()
    {
        $SubjectList = $this->SubjectModel->orderRaw("LPAD(LOWER(likes), 10,0) DESC")->limit(6)->select();
        $lunbo1 = $this->SubjectModel->order("createtime", 'desc')->paginate(3);
        $lunbo = $this->SubjectModel->find();
        $operationone = $this->SubjectModel->where("cateid=1")->order("createtime", 'desc')->paginate(5);
        $operationtwo = $this->SubjectModel->where("cateid=2")->order("createtime", 'desc')->paginate(5);
        $operationtri = $this->SubjectModel->where("cateid=3")->order("createtime", 'desc')->paginate(5);
        // var_dump('$operation');
        // exit;
   
        $this->assign([
          'lunbo1'=>$lunbo1,
          'lunbo'=>$lunbo,
        'SubjectList' => $SubjectList,
          'operationone'=>$operationone,
          'operationtwo'=>$operationtwo,
          'operationtri'=>$operationtri,

        ]);
        return $this->view->fetch();
       
    }
    
    public function search(){

          // 关键字搜索
          $keywords = $this->request->param('keywords', '', 'trim');

          // 关键搜索条件
          $where = [
              'title|content'  =>  ['like', "%$keywords%"]
          ];
  
  
          $coureslist = $this->SubjectModel->where($where)->order('createtime', 'desc')->select();
  
          $this->assign([
              'coureslist' => $coureslist,
              'keywords' => $keywords
          ]);
  
          return $this->view->fetch();
    }
    
    public function register()
    {
         // request 请求函数，isPost：判断是POST请求  param()获取请求的参数
         if($this->request->isPost())
         {
            $mobile = $this->request->param('mobile', '');
            $password = $this->request->param('password', '');
            $repass = $this->request->param('repass', '');

            // 校验密码是否一致
            if($password != $repass)
            {
                $this->error('密码不一致，请重新输入');
                exit;
            }

            //生成密码盐
            $salt = randstr(10);

            // 密码加密
            $password = md5($password.$salt);


            //组装数据
            $data= [
                'mobile' => $mobile,
                'password' => $password,
                'salt' => $salt,
                // 'sourceid'=>1
            ];

            // return Business::get(1);
            //开始插入数据库  validate 验证器  save()插入数据
            $result = $this->BusinessModel->validate('common/Business/Business')->save($data);

            // 调式sql语句
          // echo $this->BusinessModel->getLastSql();

            if($result === FALSE)
            {
                //注册失败
                // getError 返回操作的结果
                $this->error($this->BusinessModel->getError());
                exit;
            }else
            {
                //注册成功，跳转到登录界面
                $this->success('注册成功', url('home/index/login'));
                exit;
            }
        }
        
        return $this->view->fetch();
    }

    public function login()
    {
        

         if($this->request->isPost()){
            $params = $this->request->param();
            $password = $this->request->param('password', '');
            $userinfo = $this->BusinessModel->where(['mobile' => $params['mobile']])->find();
            
            if(!$userinfo){
                $this->error('该用户不存，请注册');
                exit;
            }

            // 校验密码是否正确
            $password = md5($password.$userinfo['salt']);

            if($password != $userinfo['password']){
                $this->error('密码错误，请重新输入');
                exit;
            }

            $data =[
                'id' => $userinfo['id'],
                'nickname' => $userinfo['nickname'],
                'mobile'=>$userinfo['mobile'],
            ];

            //设置cookie
            cookie('auth', $data);
           
            $this->success('登陆成功', url('/home/user/user/index'));
            exit;
            
        }
        return $this->view->fetch();
    }
     // 退出
     public function logout(){

        Cookie::delete('auth');

        $this->success('退出成功',url('home/index/login'));
        exit;
    }
   
    public function videolist()
    {
      $operationlist = $this->SubjectModel->order("createtime", 'desc')->paginate(5);

      $this->assign([
        'operationlist'=>$operationlist
      ]);

      return $this->view->fetch();
    }

    

   
}